package com.housediyadmin.view.titlewindow
{
	/**
	 * 
	 * @author wenjun cai
	 * 2010/11/24
	 */	
	import com.housediyadmin.events.SelectedFittingEve;
	import com.housediyadmin.events.brandEvents.GetBrandEvent;
	import com.housediyadmin.events.fittingEvents.GetFittingEvent;
	import com.housediyadmin.events.goodsEvents.AddGoodsEvent;
	import com.housediyadmin.events.goodsEvents.EditGoodsEvent;
	import com.housediyadmin.events.goodsEvents.GetGoodsByBrandIDEvent;
	import com.housediyadmin.model.BrandModel;
	import com.housediyadmin.model.FittingModel;
	import com.housediyadmin.model.GoodsModel;
	import com.housediyadmin.model.GoodsTreeModel;
	import com.housediyadmin.model.ListTypeModel;
	import com.housediyadmin.view.titlewindow.FittingBrandEditView;
	import com.housediyadmin.vo.Brand;
	import com.housediyadmin.vo.Fitting;
	import com.housediyadmin.vo.Goods;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.ui.Mouse;
	
	import mx.binding.utils.BindingUtils;
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.controls.DateField;
	import mx.events.CalendarLayoutChangeEvent;
	import mx.events.CloseEvent;
	import mx.formatters.DateFormatter;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.events.IndexChangeEvent;
	
	public class EditGoodsMediator extends Mediator
	{
		[Inject]
		public var view:EditGoodsView;

		[Inject]
		public var goodsModel:GoodsModel; 
	
		[Inject]
		public var XMLModel:ListTypeModel;
		
		[Bindable]
		public var topTypeArray:ArrayCollection = new ArrayCollection([{lable:"门"},{lable:"窗"},{lable:"地板"}]);
		
		override public function onRegister():void
		{
			view.nextBtn.addEventListener(MouseEvent.CLICK, nextStepHandler);
			view.cancleBtn.addEventListener(MouseEvent.CLICK, closeHandler);
			view.addEventListener(CloseEvent.CLOSE, closeHandler);	
			
			view.topTypeList.addEventListener(IndexChangeEvent.CHANGE, selectSecondType);
			view.topTypeList.dataProvider = topTypeArray;
			view.topTypeList.selectedIndex = 0;
			view.currentState = "door";
			view.secondTypeList.dataProvider = XMLModel.doorXMLList;
			view.secondTypeList.selectedIndex = 0;
		}
		
		//下一步，弹出选择fitting的界面
		private function nextStepHandler(event:MouseEvent):void
		{
			var goods:Goods = new Goods();
			goods.goodsName = view.goodsNameTxt.text;
			goods.goodsType = view.goodsTypeTxt.text;
			goods.goodsUnit = view.goodsUnitTxt.text;
			goods.goodsCount = view.goodsCountTxt.text as int;
			goods.goodsProducePlace = view.goodsProducePlaceTxt.text;
			goods.goodsBill = view.goodsBillTxt.text;
			goods.goodsGuarantee = view.goodsGuaranteeTxt.text;
			goods.goodsService = view.goodsServiceTxt.text;
			goods.goodsCreateDate = new Date();
			goods.goodsEditDate = new Date();
			
	        switch(view.topTypeList.selectedIndex)
			{
				case 0:
				goods.dictID = 1;
				goods.doorMaterial = view.doorMaterial.text;
				goods.goodsStyle = view.goodsStyleTxt.text;
				goods.isOrdered = view.isOrderedCb.selected;
				goods.isGlassed = view.isGlassedCb.selected;
				goods.isPolished = false;
				break;	
				
				case 1:
				goods.dictID = 2;
				goods.faceMaterial = view.windowMaterial.text;
				goods.isOrdered = view.isOrdered.selected;
				goods.goodsStyle = view.goodsStyle.text;
				goods.goodsFramework = view.goodsFrameworkTXT.text;
				goods.isGlassed = false;
				goods.isPolished = false;
				break;
				case 2:
					goods.dictID = 3;
					goods.goodsQuality = view.goodsQualityTxt.text;
					goods.isPolished = view.isPolished.selected;
					goods.isGlassed = false;
					goods.isOrdered = false;
					break;
			}
			goodsModel.addGoods = goods;
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
			var fittingBrandPopUp:FittingBrandEditView = new FittingBrandEditView();
			PopUpManager.addPopUp(fittingBrandPopUp, view, true);
			mediatorMap.createMediator(fittingBrandPopUp);
			PopUpManager.centerPopUp(fittingBrandPopUp);
		}
		
		//关闭取消
		private function closeHandler(event:Event):void
		{
			PopUpManager.removePopUp(view);
			mediatorMap.removeMediator( this );
		}
		
		//切换状态，绑定二级数据
		private function selectSecondType(event:IndexChangeEvent):void
		{
			switch (event.newIndex)
			{
				case 0:
					view.currentState = "door";
					view.secondTypeList.dataProvider = XMLModel.doorXMLList;
					break;
				case 1:
					view.currentState = "window";
					view.secondTypeList.dataProvider = XMLModel.windowXMLList;
					break;
				case 2:
					view.currentState = "floor";
					view.secondTypeList.dataProvider = XMLModel.floorXMLList;
					break;
			}
		}
	}
}



			








